package com.jtcoding.chat.common.enums;

import java.util.HashMap;
import java.util.Map;

/**
 * @author jason.tang
 * @create 2019-02-18 15:30
 * @description
 */
public enum ServiceEnum {
    CHAT("CHAT");

    private String code;

    private static Map<String, ServiceEnum> map;

    private ServiceEnum(String code) {
        this.code = code;
    }

    public static ServiceEnum parse(String code) throws IllegalArgumentException {

        initMap();

        ServiceEnum type = map.get(code);
        if (type != null)
            return type;

        throw new IllegalArgumentException("sender type code is undefined: " + code);
    }

    private static void initMap() {
        if (map == null) {
            map = new HashMap<String, ServiceEnum>();
            for (ServiceEnum t : ServiceEnum.values()) {
                map.put(t.code, t);
            }
        }
    }

    public String value() {
        return this.code;
    }

    @Override
    public String toString() {
        return this.code;
    }
}
